package com.qffx.moviebase.view;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import com.qffx.moviebase.R;
import com.qffx.moviebase.bean.RecommendEntity;
import com.qffx.moviebase.presenter.RecommendPresenter;
import com.qffx.moviebase.utils.common.MyRecyclerView;
import com.qffx.moviebase.adapter.RecommendAdapter;
import com.qffx.moviebase.view.impl.IRecommendView;

import fm.jiecao.jcvideoplayer_lib.JCVideoPlayer;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecommandFragment extends Fragment implements IRecommendView{
    private MyRecyclerView recyclerView;
    private RecommendPresenter recommendPresenter;
    private RecommendAdapter adapter;
    public static final String TAG="RecommandFragment";

    public RecommandFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        recommendPresenter=new RecommendPresenter(this);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view=inflater.inflate(R.layout.fragment_recommand, container, false);
        recyclerView= (MyRecyclerView) view.findViewById(R.id.fg_recommend_recyclerview);
        LinearLayoutManager manager=new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.getRefreshableView().setLayoutManager(manager);
        recyclerView.getRefreshableView().addOnScrollListener(new MYSrco());
        Window window=getActivity().getWindow();
        //取消设置透明状态栏,使 ContentView 内容不再覆盖状态栏
        window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        //需要设置这个 flag 才能调用 setStatusBarColor 来设置状态栏颜色
        window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);   window.setStatusBarColor(Color.WHITE);
        ViewGroup mContentView = (ViewGroup) getActivity().findViewById(Window.ID_ANDROID_CONTENT);
        View mChildView = mContentView.getChildAt(0);
        if (mChildView != null) {
            //注意不是设置 ContentView 的 FitsSystemWindows, 而是设置 ContentView 的第一个子 View . 预留出系统 View 的空间.
            ViewCompat.setFitsSystemWindows(mChildView, true);
        }
        recommendPresenter.getData();
        return view;
    }

    @Override
    public void setData(Object o) {
        //数据传输过来了
        RecommendEntity entity = (RecommendEntity) o;
        adapter=new RecommendAdapter(entity,getContext());
        recyclerView.getRefreshableView().setAdapter(adapter);

    }


    @Override
    public void onPause() {
        super.onPause();
        JCVideoPlayer.releaseAllVideos();
    }
    class MYSrco extends RecyclerView.OnScrollListener {
        private int _firstItemPosition = -1, _lastItemPosition;
        private View fistView, lastView;

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {

            super.onScrolled(recyclerView, dx, dy);
            RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
            //判断是当前layoutManager是否为LinearLayoutManager
            // 只有LinearLayoutManager才有查找第一个和最后一个可见view位置的方法
            if (layoutManager instanceof LinearLayoutManager) {
                LinearLayoutManager linearManager = (LinearLayoutManager) layoutManager;
                //获取最后一个可见view的位置
                int lastItemPosition = linearManager.findLastVisibleItemPosition();
                //获取第一个可见view的位置
                int firstItemPosition = linearManager.findFirstVisibleItemPosition();
                //获取可见view的总数
                int visibleItemCount = linearManager.getChildCount();

                if (_firstItemPosition < firstItemPosition) {
                    _firstItemPosition = firstItemPosition;
                    _lastItemPosition = lastItemPosition;
                    GCView(fistView);
                    fistView = recyclerView.getChildAt(0);
                    lastView = recyclerView.getChildAt(visibleItemCount - 1);
                } else if (_lastItemPosition > lastItemPosition) {
                    _firstItemPosition = firstItemPosition;
                    _lastItemPosition = lastItemPosition;
                    GCView(lastView);
                    fistView = recyclerView.getChildAt(0);
                    lastView = recyclerView.getChildAt(visibleItemCount - 1);
                }
            }

        }
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
        }

        /**
         * 回收播放
         * @param gcView
         */
        public void GCView(View gcView) {
            if (gcView != null && gcView.findViewById(R.id.fg_recommend_type2_vv) != null) {

                    JCVideoPlayer.releaseAllVideos();
                }
            }
        }
    }

